﻿using CommonExtenControl.Model;
using ManagementTool.BaseViewModels;
using ManagementTool.DB.Control;
using ManagementTool.DB.Model;
using ManagementTool.DB.Sql;
using Prism.Commands;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ManagementTool.ViewModels
{
    public class FileSelectPageViewModel: MiddleDalogViemModelBase
    {

        private Dictionary<int, int> CurrentItemIndex { set; get; }

        private bool _IsHandSearch = false;

        public bool IsHandSearch
        {
            get
            {
                return _IsHandSearch;
            }
            set
            {
                SetProperty(ref _IsHandSearch, value);
            }
        }

       

        private ObservableCollection<CommonExtenControl.Model.FileModel> _FileList;

        public ObservableCollection<CommonExtenControl.Model.FileModel> FileList
        {
            get { return _FileList; }
            set { SetProperty(ref _FileList, value); }
        }


        private CommonExtenControl.Model.FileModel _SelectFile;

        public CommonExtenControl.Model.FileModel SelectFile
        {
            get { return _SelectFile; }
            set { SetProperty(ref _SelectFile, value); }
        }


        private DelegateCommand _BackPageCommand;
        public DelegateCommand BackPageCommand =>
            _BackPageCommand ?? (_BackPageCommand = new DelegateCommand(BackPageCommandMethod));


        private DelegateCommand<int?> _PageUpdatedCommand;
        public DelegateCommand<int?> PageUpdatedCommand =>
            _PageUpdatedCommand ?? (_PageUpdatedCommand = new DelegateCommand<int?>(ExecutePageUpdatedCommand));

        private DelegateCommand<FileModel> _DataGridMouseDoblickCommand;
        public DelegateCommand<FileModel> DataGridMouseDoblickCommand =>
            _DataGridMouseDoblickCommand ?? (_DataGridMouseDoblickCommand = new DelegateCommand<FileModel>(DataGridMouseDoblickCommandMethod));

        private void DataGridMouseDoblickCommandMethod(FileModel model)
        {
            BackToPage(ButtonResult.OK, model);
        }

        private async void ExecutePageUpdatedCommand(int? JumpIndex)
        {
            await Task.Run(() => {

            });
        }

        public override void OnDialogClosed()
        {
            base.OnDialogClosed();
        }


        public override void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            CurrentItemIndex = new Dictionary<int, int>();
            FileList = new ObservableCollection<CommonExtenControl.Model.FileModel>();
            Task.Run(() => {
                GetAllCount();
                GetDevListFromDB(1);
            });
        }


        private void GetAllCount()
        {
            string _AllCountSql = InstanceBase<SqlsControl>.Instance.GetDeviceInfoDBCountSql();
            PageCount = InitDate(_AllCountSql);
            PageItemsCount = Limits.FirstOrDefault();
        }

        private void GetDevListFromDB(int _currentPageIndex)
        {
            try
            {
                string _str_sql = InstanceBase<SqlsControl>.Instance.GeFileInfoDBSql(_currentPageIndex, PageItemsCount);
                var _dataSet = InstanceBase<MySqlDbControl>.Instance.ExecuteQuery(_str_sql);
                int SumIndex = GetSumIndex(1);
                IList<FileModel> resultList = ModelConvertHelper<FileModel>.ConvertToModel(_dataSet);

                if (resultList.Count > 0)
                {
                    System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => {
                        FileList.Clear();
                        foreach (var item in resultList)
                        {
                            SumIndex++;
                            var itemModel = new CommonExtenControl.Model.FileModel();
                            itemModel.Index = SumIndex;
                            itemModel.FileName = item.FileName;
                            itemModel.FilePath = item.FilePath;
                            itemModel.FileUpdateTime = item.FileUpdateTime;
                            itemModel.FileVersion = item.FileVersion;
                            FileList.Add(itemModel);
                        }
                    }));
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        protected virtual int GetSumIndex(int _pageIndex)
        {
            int sum = 0;
            int _SumIndex = 0;
            try
            {
                CurrentItemIndex.Clear();
                int sumCunt = SumPageCount / PageItemsCount + 1;
                for (int i = 1; i <= sumCunt; i++)
                {
                    CurrentItemIndex.Add(i, _SumIndex);
                    _SumIndex += PageItemsCount;
                }


                if (CurrentItemIndex.Count > 0)
                {
                    sum = CurrentItemIndex[_pageIndex];
                }
            }
            catch (Exception ex)
            {
               
            }
            return sum;
        }


        protected int InitDate(string _AllCountSql)
        {
            PageItemsCount = Limits.FirstOrDefault();

            return GetAllDataCount(_AllCountSql);
        }

        protected int GetAllDataCount(string _sql)
        {
            var itemTab = InstanceBase<MySqlDbControl>.Instance.ExecuteQuery(_sql);
            try
            {
                if (itemTab.Rows.Count > 0)
                {
                    SumPageCount = Convert.ToInt32(itemTab.Rows[0].ItemArray[0]);
                }
            }
            catch (Exception ex)
            {
                SumPageCount = 0;
              
            }
            itemTab.Dispose();
            return SumPageCount;
        }

        private void BackPageCommandMethod()
        {
            BackToPage(ButtonResult.Cancel, null);
        }

        protected void BackToPage(ButtonResult _ButtonResult, FileModel _ItemDate = null)
        {
            try
            {
                var itemParam = new DialogParameters();
                itemParam.Add("FileName", _ItemDate.FileName);
                itemParam.Add("FilePath", _ItemDate.FilePath);
                itemParam.Add("FileVersion", _ItemDate.FileVersion);
                var itemRsult = new DialogResult(_ButtonResult, itemParam);
                HandClose(itemRsult);
            }
            catch (Exception ex)
            {
              
            }
        }

    }
}
